﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ContenutiDinamiciBLL;
using Ext.Net;

namespace TestSuiteExtNET
{
    public partial class Demo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TestLoad();
            //LoadContesti();

            if (!Page.IsPostBack)
            {
                LoadContesti();
            }
        }

        private void TestLoad()
        {
            ContenutiDinamiciDAL.ContenutiDinamiciController cdContr = new ContenutiDinamiciDAL.ContenutiDinamiciController();
            List<Context> m_contexts = (List<Context>)cdContr.GetContexts();

            Ext.Net.TreePanel p = new Ext.Net.TreePanel();
            p.Title = string.Format("Contesto {0}", "BPP");
            p.Width = Unit.Pixel(300);
            p.Height = Unit.Pixel(450);
            this.rContesti.Items.Add(p);

            List<Context> m_contexts1 = (List<Context>)cdContr.GetContext("BPP");
            Ext.Net.Node root = new Ext.Net.Node()
            {
                Text = "BPP"
            };
            //root.Expanded = true;
            p.Root.Add(root);

            //foreach (Context ctx in m_contexts1)
            //{
            //    Ext.Net.Node n = new Node() { Text = ctx.Name };
            //    root.Children.Add(n);
            //}
        }

        private void LoadContesti()
        {
            ContenutiDinamiciDAL.ContenutiDinamiciController cdContr = new ContenutiDinamiciDAL.ContenutiDinamiciController();
            List<Context> m_contexts = (List<Context>)cdContr.GetContexts();

            List<Ext.Net.TreePanel> m_contesti = new List<Ext.Net.TreePanel>();

            foreach (Context ctx in m_contexts)
            {
                if (ctx.ID != Guid.Empty)
                {
                    Ext.Net.TreePanel p = new Ext.Net.TreePanel();
                    p.Title = string.Format("Contesto {0}", ctx.SiteCode);
                    p.ID = ctx.ID.ToString();
                    //p.DirectEvents.BeforeExpand.Event += new ComponentDirectEvent.DirectEventHandler(BeforeExpand_Event);
                    //p.DirectEvents.BeforeExpand.ExtraParams.Add(new Ext.Net.Parameter("ID", p.ID));
                    //p.DirectEvents.BeforeExpand.ExtraParams.Add(new Ext.Net.Parameter("SiteCode", ctx.SiteCode));

                    p.Root.Add(CreateRoot(ctx.ID.ToString(), ctx.SiteCode));
                    //p.RootVisible = false;

                    p.Listeners.BeforeLoad.Fn = "nodesLoad";
                    p.Listeners.ItemClick.Fn = "nodeClicked";

                    //
                    m_contesti.Add(p);
                }
            }

            this.rContesti.Items.Add(m_contesti);
        }

        private Ext.Net.Node CreateRoot(string id, string text)
        {
            Ext.Net.Node retValue = new Ext.Net.Node()
            {
                Text = text,
                Icon = Icon.None,
                NodeID = text,
            };

            retValue.CustomAttributes.Add(new ConfigItem("ID", id));

            return retValue;
        }

        void BeforeExpand_Event(object sender, DirectEventArgs e)
        {
            string m_siteCode = e.ExtraParams["SiteCode"];

            LoadContestiPerSito(sender, m_siteCode);
        }

        private void LoadContestiPerSito(object sender, string siteCode)
        {
            Ext.Net.TreePanel tv = (Ext.Net.TreePanel) sender; 

            ContenutiDinamiciDAL.ContenutiDinamiciController cdContr = new ContenutiDinamiciDAL.ContenutiDinamiciController();
            List<Context> m_contexts = (List<Context>)cdContr.GetContext("BPP");

            //Ext.Net.TreePanel tv = new Ext.Net.TreePanel();
            Ext.Net.Node root = new Ext.Net.Node()
            {
                Text = siteCode,
                Icon = Icon.None,
            };
            root.Expanded = true;
            tv.Root.Add(root);
            tv.Body.Mask("loading...");

            //tv.RootVisible = false;

            foreach (Context ctx in m_contexts)
            {
                Ext.Net.Node n = new Node() { Text = ctx.Name, Leaf=true };
                root.Children.Add(n);
            }

            tv.Render();           
            //((Ext.Net.Panel)sender).Items.Add(tv);
            //((Ext.Net.Panel)sender).UpdateContent();
        }

        [DirectMethod]
        public static string NodesLoad(string siteCode)
        {
            NodeCollection nodes = new Ext.Net.NodeCollection();

            ContenutiDinamiciDAL.ContenutiDinamiciController cdContr = new ContenutiDinamiciDAL.ContenutiDinamiciController();
            List<Context> m_contexts = (List<Context>)cdContr.GetContext(siteCode);

            foreach (Context ctx in m_contexts)
            {
                Ext.Net.Node n = new Node() { Text = ctx.Name, Leaf = true, Icon = Icon.None };
                n.NodeID = ctx.ID.ToString();
                nodes.Add(n);
            }

            return nodes.ToJson();
        }

        [DirectMethod]
        public void NodeClicked(string nodeId)
        {
            NodeCollection nodes = new Ext.Net.NodeCollection();

            ContenutiDinamiciDAL.ContenutiDinamiciController cdContr = new ContenutiDinamiciDAL.ContenutiDinamiciController();
            List<Detail> m_contextDetails = (List<Detail>)cdContr.GetContextDetails(nodeId);

            if (m_contextDetails != null)
            {
                if (m_contextDetails.Count() == 1)
                {
                    Ext.Net.FormPanel fp = new FormPanel();
                    fp.Title = string.Format("Descrizione: {0}", m_contextDetails[0].Description);

                    //this.rDettagli.Items.Clear();
                    //this.rDettagli.Items.Add(fp);
                    //this.rDettagli.UpdateContent();
                }
                else
                {
                    if (m_contextDetails.Count() > 1)
                    {
                        //Ext.Net.GridPanel grid = new GridPanel();

                        //Ext.Net.Model m_model = new Model();

                        //ModelField m_modelField = new ModelField();
                        //m_modelField.Name = "Description";

                        //m_model.Fields.Add(m_modelField);

                        //Store m_store = new Store();
                        //m_store.Model.Add(m_model);
                        //m_store.DataSource = m_contextDetails;
                        
                        ////m_store.DataBind();

                        //this.grid1.Store.Add(m_store);
                        //this.grid1.DataBind();

                        ////grid.Store.Add(m_store);
                        ////grid.DataBind();

                        ////this.rDettagli.Items.Clear();
                        ////this.rDettagli.Items.Add(grid);
                        //this.rDettagli.UpdateContent();
                    }
                }
            }
        }
    }
}